from typing import Optional

from model.uml_element import UmlElement
from model.uml_value import UmlValue
from model.uml_type import UmlType
from model.uml_xmi_info import UmlXmiInfo


class UmlProperty(UmlElement):
    def __init__(self) -> None:
        super().__init__(UmlXmiInfo.type_property)
        self.name: str = ""
        self.aggregation: str = ""

        """
        The data type of the property
        """
        self.type: Optional[UmlType] = None

        """
        association: association id
        """
        self.association: str = ""
        self.isDerived: str = ""
        self.isDerivedUnion: str = ""
        self.isOrdered: str = ""
        self.isReadOnly: str = ""
        self.isUnique: str = ""
        # TODO: 枚举类型
        self.visibility: str = ""
        self.redefinedProperty: str = ""
        self.subsettedProperty: str = ""

        self.defaultValue: Optional[UmlValue] = None
        self.lowerValue: Optional[UmlValue] = None
        self.upperValue: Optional[UmlValue] = None
        
        # Transform 中使用的变量
        self.locked = False

    def to_json(self) -> dict[str, object]:
        json: dict = super().to_json()
        json["name"] = self.name
        json["aggregation"] = self.aggregation
        json["association"] = self.association
        json["isDerived"] = self.isDerived
        json["isDerivedUnion"] = self.isDerivedUnion
        json["isOrdered"] = self.isOrdered
        json["isReadOnly"] = self.isReadOnly
        json["isUnique"] = self.isUnique
        json["redefinedProperty"] = self.redefinedProperty
        json["subsettedProperty"] = self.subsettedProperty

        if self.type is not None:
            json["type"] = self.type.to_json()
        if self.ownedComment is not None:
            json["ownedComment"] = self.ownedComment.to_json()
        if self.defaultValue is not None:
            json["defaultValue"] = self.defaultValue.to_json()
        if self.lowerValue is not None:
            json["lowerValue"] = self.lowerValue.to_json()
        if self.upperValue is not None:
            json["upperValue"] = self.upperValue.to_json()

        return json
